/*
 * @Author: many
 * @Date: 2020-06-21 17:13:42
 * @LastEditors: di
 * @LastEditTime: 2020-06-22 17:34:16
 * @Description: file content
 */ 
import axios from 'axios'
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000 // request timeout
})
import { Toast } from 'vant'
// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent

    // if (store.getters.token) {
    //   config.headers['X-Token'] = getToken()
    // }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
// response interceptor
service.interceptors.response.use(
  response => {
    // console.log("response", response)
    const resData = response.data;
    const whiteList =  ['200']
    if (whiteList.includes(resData.resultCode)) {
     return Promise.resolve(resData.resultData)
    } else {
      Toast.fail(resData.resultMsg || '操作失败');
      return  Promise.reject(resData)
    }
    
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
